package com.itic.appbase.framework.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.itic.appbase.framework.utils.configuration.PropertyHelper;

/**
 * <p>
 * Company: itic
 * </p>
 * 
 * @author zhaokechun
 * @date 2014年8月5日 下午2:17:52
 * @version 1.0
 */
public class UploadUtils {
    private UploadUtils() {
        super();
    }

    /**
     * 
     * 获取上传文件的根目录 格式: 用户主目录/项目编号_fileupload(若未配置项目编号则为fileupload)
     * 
     * @return
     */
    public static String getRootDir() {
        String uploadPath = PropertyHelper.getString("app.upload.path");
        String userHome = System.getProperty("user.home");
        if (StringHelper.isNotBlank(uploadPath)) {
            userHome = uploadPath;
        }
        String projectName = PropertyHelper.getString("app.code");
        StringBuilder rootDir = new StringBuilder();
        rootDir.append(userHome).append("/").append(projectName == null ? "" : (projectName + "_"))
                .append("fileupload").append("/");
        return rootDir.toString();
    }
    
    
   
    public static void upload(HttpServletRequest request,String filePath){
		     CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
	                  request.getSession().getServletContext());
			 
			 if( multipartResolver.isMultipart(request) ){
				 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				 FileOutputStream fileOutputStream = null;
				 try {
 					  multiRequest.setCharacterEncoding(request.getCharacterEncoding());
 					  //ServletInputStream input = multiRequest.getInputStream();
 					  // 获得文件：   
 				      MultipartFile file = multiRequest.getFile("file");   
 				      // 获得文件名：   
 				      String filename = file.getOriginalFilename();   
 				      String _xxx = filename.substring(filename.lastIndexOf(".")+1);
 				      String uuid = IdGen.uuid();
 				      String name = uuid+"."+_xxx;
 				      // 获得输入流：   
 				      InputStream input = file.getInputStream(); 
 				      //获得相对路径
 				      //String contextPath = request.getContextPath();
 				      //String realPath = request.getSession().getServletContext().getRealPath(File.separator) ;
 				      //String rootDir = FileHelper.getRootDir();
 				      //String rootDir = getValueByKey(request,"upload_path");
 				      //String time = getDateStringByDate( new Date(),"yyyy-MM-dd" );
 				      //String filePath = rootDir+"/"+time+"/";
 				      
 				      File f=new File(filePath);
 				      if(!f.exists()){
 				    	  f.mkdirs();
 				      }
 				      
 				      File tempfile =new File(filePath+name);
 				      
 				      if(!tempfile.exists()){
 				    	  tempfile.createNewFile();
 				      }
 				      int i=0;
 			          fileOutputStream = new FileOutputStream(tempfile); 
 			          
 			          while ((i = input.read())!=-1){  
 			            	fileOutputStream.write(i);
 			          }  
 			        
				      }catch(Exception e){
				    	  e.printStackTrace();
				      }
			   }
    }
}
